//
//  TimeUnit.m
//  RSToolNSObject
//
//  Created by thinkjoy on 16/9/24.
//  Copyright © 2016年 Thinkjoy_RS. All rights reserved.
//

#import "TimeUnit.h"


#define IsNilOrNull(_ref)            (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) || ([(_ref) length]==0) || ([(_ref)isEqualToString:@" "]))

#pragma mark - 日志开关
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif

@implementation TimeUnit

# pragma mark   时间戳转化为时间
+ (NSString *)GetTimeStrWithTimestamp:(NSString * )timestamp withFormatterStyle:(NSString *)formatterStr{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:[formatterStr length]==0?@"YYYY-MM-dd HH:mm":formatterStr];
    //时间戳转换为时间
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]/1000];
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
}

# pragma mark   当前时间转化为时间字符串
+ (NSString *)getNowTimeStrWithNowTimeWithFormatterStyle:(NSString *)formatterStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:[formatterStr length]==0?@"YYYY-MM-dd HH:mm":formatterStr];
    NSDate *date = [NSDate date];
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
}

#pragma mark    根据一个NSDate获得当前是星期几
+ (NSString *)getWeekdayStringFromDate:(NSDate *)inputDate
{
    if (inputDate==nil) {
        inputDate   =   [NSDate date];
    }
    NSArray *weekdays = [NSArray arrayWithObjects:[NSNull null],@"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
}

# pragma mark   根据一个秒数计算出dd天HH时mm分
+ (NSString *)getTheDayStrWithSeconds:(NSTimeInterval)secs
{
    NSString    * str = nil;
    
    NSInteger day   =  (long)secs/60/60/24;
    NSInteger hour  =  (long)secs/60/60%24;
    NSInteger minutes = (long)secs/60%60;
    
    //    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //    NSDate *date = [NSDate dateWithTimeIntervalSince1970:secs];
    
    
    if (day>0) {
        //        [formatter setDateFormat:@"dd天HH时mm分"];
        
        str =   [NSString stringWithFormat:@"%ld天%02ld时%02ld分",(long)day,hour,minutes];
    }else if (hour>0){
        //        [formatter setDateFormat:@"HH时mm分"];
        str =   [NSString stringWithFormat:@"%02ld时%02ld分",(long)hour,minutes];
    }else if (minutes>0){
        //        [formatter setDateFormat:@"mm分"];
        str =   [NSString stringWithFormat:@"%02ld分",(long)minutes];
    }else{
        return @"0分";
    }
    
    return str;
}

#pragma mark比较两个时间字符串大小,开始时间大于等于结束时间返回YES;当两个字符串只要有一个是空就返回NO
+ (BOOL)compareTwoTimeStrWithStartStr:(NSString *)startSyr  withEndStr:(NSString *)endStr
{
    //    NSDateForma/
    
    if (startSyr.length!=0&&startSyr!=nil&&endStr.length!=0&&endStr!=nil) {
        switch ([startSyr compare:endStr]) {
            case NSOrderedAscending:
                DLog(@"后面一个字符串大于前面一个");
                return NO;
                break;
            case NSOrderedDescending:
                DLog(@"后面一个字符串小于前面一个");
                return YES;
                break;
            case NSOrderedSame:
                DLog(@"两个字符串一样");
                return YES;
                break;
        }
        //
        //        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        //
        //        NSDate  * startDate =   [formatter dateFromString:startSyr];
        //        CGFloat startSp   =    [startDate timeIntervalSince1970];
        //
        //
        //        NSDate  * endDate =   [formatter dateFromString:endStr];
        //        CGFloat endSp   =    [endDate timeIntervalSince1970];
        //
        //        NSLog(@"%@--%@---%f======%@--%@---%f",startSyr,startDate,startSp,endStr,endDate,endSp);
        //        if (startSp>=endSp) {
        //
        //            return YES;
        //        }else{
        //            return NO;
        //        }
        
    }else{
        return NO;
    }
    
}

#pragma mark    通过时间获得时间戳
/**
 *  通过时间获得时间戳
 */
+ (NSInteger)getTimestampWithDate:(NSDate *)date
{
    if (date==nil) {
        date    =   [NSDate date];
    }
    return [date timeIntervalSince1970];
}
@end
